/* Program name: lastcurrent_dates.sas*/
/* Objective: create a variable clled currdate that is the date that a deqlineunt balance was last not delinquent (current)*/
/* This code requires the mopdate variable createdd in clean_tradeline01.sas, so it should be run after those programs*/

OPTIONS LINESIZE=79;

libname local '/data';
libname out '/data';
%include "/data/tu_formats.sas";

%macro makedates(yr);
data out.cleantrade&yr.;
     set out.interestrate&yr.;
     format mopdate datecurr datefirst120 yymmn6.;

     * find the first time the account was last recent;
     if mop ge 2 then do;
	   if dtclose gt 0 then mopdate = input(put(dtclose,6.),yymmn6.);
	   if dtclose le 0 and dtpdout gt 0 then mopdate = input(put(dtpdout,6.),yymmn6.);
	   if dtclose le 0 and dtpdout le 0 then mopdate = input(put(dtveri,6.),yymmn6.);
	   statuscurr=mop;
	   monthscurr=0;
	   do until (statuscurr=1 or monthscurr=49);
	      monthscurr=monthscurr+1;
	      if substr(paypat,monthscurr,1) eq 'X' then continue;
	      statuscurr=input(substr(paypat,monthscurr,1),best12.);
	      end;
     end;

     *find the first time account was 120+dpd;
     if mop = 5 then do;
	   if dtclose gt 0 then mopdate = input(put(dtclose,6.),yymmn6.);
	   if dtclose le 0 and dtpdout gt 0 then mopdate = input(put(dtpdout,6.),yymmn6.);
	   if dtclose le 0 and dtpdout le 0 then mopdate = input(put(dtveri,6.),yymmn6.);
	   statusf120=mop;
	   monthsf120=0;
	   do until (statusf120 ^= 5 or monthsf120=49);
	      monthsf120=monthsf120+1;
	      if substr(paypat,monthsf120,1) eq 'X' then continue;
	      statusf120=input(substr(paypat,monthsf120,1),best12.);
	      end;
     end;

     * create the dates;	
     if statuscurr=. then monthscurr=.;
     if statusf120=. then monthsf120=.;
     datecurr =  intnx('month',mopdate,-monthscurr);
     datefirst120 =  intnx('month',mopdate,-monthsf120);
     run;
*To look at things to make sure they are working correctly;
*proc print data= out.cleantrade99 (obs=10);
*     var teditseq paypat mop mopdate monthscurr datecurr monthsf120 datefirst120;
*     where not missing(datecurr) or not missing(datefirst120);
*     run;
%mend;

%makedates(97);
%makedates(99);
%makedates(01);
%makedates(03);
%makedates(04); 
%makedates(07);
%makedates(08);
%makedates(10);
endsas;












*oldcode;

OPTIONS LINESIZE=79;

libname out '/data';
%include "/data/tu_formats.sas";

%macro makedates(yr);
data out.cleantrade&yr.;
     set out.interestrate&yr.;
     format mopdate currdate yymmn6.;
     status=mop;
     months=0;
     if mop ge 2 then do;
	   if dtclose gt 0 then mopdate = input(put(dtclose,6.),yymmn6.);
	   if dtclose le 0 and dtpdout gt 0 then mopdate = input(put(dtpdout,6.),yymmn6.);
	   if dtclose le 0 and dtpdout le 0 then mopdate = input(put(dtveri,6.),yymmn6.);
	   status=mop;
	   months=0;
	   do until (status=1 or months=49);
	      months=months+1;
	      if substr(paypat,months,1) eq 'X' then continue;
	      status=input(substr(paypat,months,1),best12.);
	      end;
     end;
     if status=. then months=.;
     currdate =  intnx('month',mopdate,-months);
run;
*To look at things to make sure they are working correctly;
*proc print data= trade&yr. (obs=10);
*     var teditseq paypat mop mopdate status months currdate;
*     run;
%mend;
%makedates(01);
%makedates(97);
%makedates(99);
%makedates(01);
%makedates(03);
%makedates(04);
%makedates(07);
%makedates(08);
%makedates(10);
